package com.hxht.cmp.controller;

import com.hxht.cmp.common.Constants;
import com.hxht.cmp.common.enums.ResultCode;
import com.hxht.cmp.common.result.ResultResponse;
import com.hxht.cmp.entity.Court;
import com.hxht.cmp.entity.User;
import com.hxht.cmp.pojo.BasisCourt;
import com.hxht.cmp.service.BasisService;
import com.hxht.cmp.shiro.ShiroPermissionFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author alice on 2017/6/29 0029.
 * @version 1.0
 * @since 1.0
 */
@Controller
@RequestMapping("/basis")
public class BasisController {
    private final Logger log = LoggerFactory.getLogger(BasisController.class);
    @Resource
    private BasisService basisService;
    @Resource
    private ShiroPermissionFactory shiroPermissionFactory;

    /**
     * 刷新基础配置.
     * 刷新内容为config
     *
     * @return ResultResponse
     */
    @RequestMapping(value = "/flushDict")
    public @ResponseBody
    ResultResponse flushDict() {
        return basisService.flushDict() ? ResultResponse.success() : ResultResponse.failed();
    }

    /**
     * 刷新全部基础配置.
     * 刷新内容包括config,法院列表,法庭列表,案件类型,角色列表
     *
     * @return ResultResponse
     */
    @RequestMapping(value = "/flushAll")
    public @ResponseBody
    ResultResponse flushAll() {
        log.info("刷新所有项目基础设置");
        try {
            basisService.init();
            shiroPermissionFactory.reloadShiro();
            log.info("刷新所有项目基础设置成功");
            return ResultResponse.success();
        } catch (Exception e) {
            Constants.SYSTEM_LOCKED = 1;
            return ResultResponse.failed(ResultCode.SERVER_ERROR);
        }
    }

    @GetMapping(value = "/flushGet")
    public @ResponseBody
    ResponseEntity flush() {
        log.info("刷新所有项目基础设置");
        try {
            basisService.init();
            log.info("不刷新权限成功");
            return new ResponseEntity(HttpStatus.OK);
        } catch (Exception e) {
            Constants.SYSTEM_LOCKED = 1;
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @ModelAttribute("courts")
    public List<Court> courts() {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()) {
            List<Court> result = new ArrayList<>();
            Session session = currentUser.getSession();
            User user = (User) session.getAttribute("user");
            Court court = Constants.COURT_MAP.get(user.getCourtUid());
            String courtUid = court.getUid();
            if (!Objects.equals(courtUid, Constants.CENTRAL_COURT.getUid())) {
                for (Court tempCourt : Constants.COURT_LIST) {
                    if (Objects.equals(courtUid, tempCourt.getUid()) || Objects.equals(court.getUid(), tempCourt.getParentUid())) {
                        result.add(tempCourt);
                    }
                }
                return result;
            } else {
                return Constants.COURT_LIST;
            }
        }
        return null;
    }

    @RequestMapping(value = "/getCourtList", method = RequestMethod.GET)
    public @ResponseBody
    ResultResponse getCourtList(String courtUid) {
        if (ObjectUtils.isEmpty(courtUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        List<BasisCourt> result = new ArrayList<>();
        Court court = Constants.COURT_MAP.get(courtUid);
        if (!Objects.equals(courtUid, Constants.CENTRAL_COURT.getUid())) {
            for (BasisCourt tempCourt : Constants.COURT_CONFIG_LIST) {
                if (Objects.equals(courtUid, tempCourt.getUid()) || Objects.equals(court.getUid(), tempCourt.getParentUid())) {
                    result.add(tempCourt);
                }
            }
        } else {
            return ResultResponse.success(Constants.COURT_CONFIG_LIST);
        }
        return ResultResponse.success(result);
    }
}
